package com.niiwoo.civet.mobile.service.bill.detail;

import com.alibaba.fastjson.JSON;
import com.niiwoo.civet.account.dto.response.NiiwooBillRespDTO;
import com.niiwoo.civet.account.dto.response.UserBillBaseResponseDTO;
import com.niiwoo.civet.account.enums.BillBusinessTypeEnum;
import com.niiwoo.civet.account.enums.OrderTypeEnum;
import com.niiwoo.civet.mobile.vo.response.bill.UserBillDetailResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * Created by xiaoyingping on 2018/11/7.
 */
@Service
public class BillDetailStrategyHandle {

    @Autowired
    private InvestBillDetailStrategyImpl investBillDetailStrategy;
    @Autowired
    private LoanBillDetailStrategyImpl loanBillDetailStrategy;
    @Autowired
    private RecerivedBillDetailStrategyImpl recerivedBillDetailStrategy;
    @Autowired
    private RechargeBillDetailStrategyImpl rechargeBillDetailStrategy;
    @Autowired
    private RefundBillDetailStrategyImpl refundBillDetailStrategy;
    @Autowired
    private RepaymentBillDetailStrategyImpl repaymentBillDetailStrategy;
    @Autowired
    private PrizeBillDetailStrategyImpl prizeBillDetailStrategy;
    @Autowired
    private TransferBillDetailStrategyImpl transferBillDetailStrategy;
    @Autowired
    private WithdrawBillDetailStrategyImpl withdrawBillDetailStrategy;
    @Autowired
    private FundTransferBillDetailStrategyImpl fundTransferBillDetailStrategy;

    /**
     * 组装账单详情
     * @param response
     * @return
     */
    public String buildBillDetail(UserBillBaseResponseDTO userBillBase, List<NiiwooBillRespDTO> userBillFlowList, UserBillDetailResponse response){
        //指定策略
        BillDetailStrategy strategy = createStrategy(userBillBase);

        //按策略组装详情
        if(Objects.isNull(strategy)){
            return JSON.toJSONString(response);
        }else {
            return strategy.build(userBillBase, userBillFlowList, response);
        }

    }



    //指定策略
    private BillDetailStrategy createStrategy(UserBillBaseResponseDTO userBillBase){
        BillDetailStrategy strategy = null;

        OrderTypeEnum orderTypeEnum = OrderTypeEnum.enumOf(userBillBase.getOrderType());
        if(Objects.nonNull(orderTypeEnum)){
            BillBusinessTypeEnum billBusinessTypeEnum = orderTypeEnum.getBillBusinessTypeEnum();
            if(BillBusinessTypeEnum.INVEST == billBusinessTypeEnum){
                strategy = investBillDetailStrategy;
            }else if(BillBusinessTypeEnum.LEND == billBusinessTypeEnum){
                strategy = loanBillDetailStrategy;
            }else if(BillBusinessTypeEnum.RECEIVED_REPAYMENT == billBusinessTypeEnum){
                strategy = recerivedBillDetailStrategy;
            }else if(BillBusinessTypeEnum.RECHARGE == billBusinessTypeEnum){
                strategy = rechargeBillDetailStrategy;
            }else if(BillBusinessTypeEnum.WITHDRAW == billBusinessTypeEnum){
                strategy = withdrawBillDetailStrategy;
            }else if(BillBusinessTypeEnum.REPAYMENT == billBusinessTypeEnum){
                strategy = repaymentBillDetailStrategy;
            }else if(BillBusinessTypeEnum.PRIZED == billBusinessTypeEnum){
                strategy = prizeBillDetailStrategy;
            }else if(BillBusinessTypeEnum.CREDITOR_TRANSFER == billBusinessTypeEnum){
                strategy = transferBillDetailStrategy;
            }else if(BillBusinessTypeEnum.OTHER == billBusinessTypeEnum){
                if(OrderTypeEnum.FUND_TRANSFER == orderTypeEnum){
                    strategy = fundTransferBillDetailStrategy;
                }
            }

            if(Objects.nonNull(userBillBase.getBackSourceBillId())){
                //资金回退
                strategy = refundBillDetailStrategy;
            }
        }

        return strategy;
    }
}
